import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/address_book_info_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class AddressBookSearch extends StatefulWidget {
  @override
  _AddressBookSearchState createState() => _AddressBookSearchState();
}

class _AddressBookSearchState extends State<AddressBookSearch> {
  TextEditingController searchContro = new TextEditingController();
  bool show = true;
  List<AddressBookInfoData> datas = [];
  bool loadding = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        leading: null,
        title: null,
        actions: <Widget>[
          Container(
            width: 40,
            child: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          ),
          Container(
            width: MediaQuery.of(context).size.width - 90,
            margin: EdgeInsets.only(left: 0, right: 0, top: 8, bottom: 8),
            padding: EdgeInsets.only(left: 20, top: 10, bottom: 10),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(25.0),
            ),
            child: TextField(
              controller: searchContro,
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                hintText: "请输入",
                contentPadding: EdgeInsets.all(0),
                hintStyle: TextStyle(color: Colors.black26, fontSize: 14),
                border: InputBorder.none,
              ),
            ),
          ),
          Center(
            child: Container(
              width: 50,
              margin: EdgeInsets.only(left: 0),
              padding: EdgeInsets.only(left: 10, right: 10),
              child: GestureDetector(
                onTap: () {
                  if (searchContro.text == null || searchContro.text.isEmpty) {
                    TostUtil.showToast("请输入搜索内容");
                  } else {
                    searchInfo(searchContro.text);
                  }
                },
                child: Text("搜索", style: TextStyle(fontSize: 13)),
              ),
            ),
          ),
        ],
      ),
      body: LoadBuilder(
        visibility: loadding,
        builder: Stack(
          children: <Widget>[
            Visibility(
              child: ListView.builder(
                itemBuilder: (context, index) {
                  return Container(
                    color: Colors.white,
                    child: ExpansionTile(
                        backgroundColor: Colors.white70,
                        initiallyExpanded: true,
                        title: Text("${datas[index].departmentname}"),
                        children: datas[index]
                            .userList
                            .map((info) => ListTile(
                                  title: Text(info.username),
                                  subtitle: Text(info.useraccount),
                                  onTap: () {
                                    Navigator.pushNamed(
                                        context, "/personal_data_page",
                                        arguments: {
                                          "id": info.userid,
                                          "other": true
                                        });
                                  },
                                ))
                            .toList()),
                  );
                },
                itemCount: datas.length,
              ),
              visible: !show,
            ),
            Visibility(
                visible: show,
                child: Center(
                  child: Text(
                    "暂无结果",
                    style: TextStyle(color: Colors.blueGrey, fontSize: 20),
                  ),
                )),
          ],
        ),
      ),
    );
  }

  void searchInfo(info) {
    setState(() {
      loadding = true;
    });
    InternetAPI.getInstance()
        .get("WConfiguration/PptUser/GetDepartmentUserSelectItemList", (data) {
      setState(() {
        loadding = false;
        datas = AddressBookInfoEntity.fromJson(data).datas;
        if (datas == null || datas.length == 0) {
          show = true;
        } else {
          show = false;
        }
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "queryuser": info,
    });
  }
}
